package com.attendance.www.hr.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.LactationBean;
import com.attendance.www.hr.beans.LactationPageBean;
import com.attendance.www.hr.dao.mapper.LactationExtMapper;
import com.attendance.www.hr.dao.mapper.LactationMapper;
import com.attendance.www.hr.dao.mapper.RequestAuthorityMapper;
import com.attendance.www.hr.dao.mapper.RequestReportMapper;
import com.attendance.www.hr.entitys.Lactation;
import com.attendance.www.hr.entitys.LactationExample;
import com.attendance.www.hr.entitys.RequestAuthority;
import com.attendance.www.hr.entitys.RequestAuthorityExample;
import com.attendance.www.hr.entitys.RequestReport;
import com.attendance.www.hr.entitys.RequestReportExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.ILactationService;

@Service
public class LactationServiceImpl implements ILactationService {

	@Resource
	private LactationMapper lactationDao;
	@Resource
	private LactationExtMapper lactationExtDao;
	@Resource
	private RequestAuthorityMapper requestAuthorityDao;
	@Resource
	private RequestReportMapper requestReportDao;
	@Resource
	private IApprovalService approvalService;
	
	/**
	 * 新增验证时间是否重叠
	 */
	@Override
	public boolean getVerifySave(LactationBean lactationBean) {
		int employeeId=lactationBean.getEmployeeId();
		//孩子的出生日期，（本次的开始时间）
		Date bornDate=lactationBean.getHbornStartDate();
		//哺乳假结束日期
		Date endDate=lactationBean.getEndDate();
		LactationExample lactationExample=new LactationExample();
		//开始时间是否在区间内
		lactationExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andHbornStartDateLessThanOrEqualTo(bornDate)
		.andEndDateGreaterThanOrEqualTo(bornDate);
		List<Lactation> list=lactationDao.selectByExample(lactationExample);
		if (list.size()>0) {
			return true;
		}
		//哺乳假结束日期是否在区间内
		lactationExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andHbornStartDateLessThanOrEqualTo(endDate)
		.andEndDateGreaterThanOrEqualTo(endDate);
		list=lactationDao.selectByExample(lactationExample);
		if (list.size()>0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 新增哺乳假
	 */
	@Override
	public boolean saveLactation(LactationBean lactationBean) {
		Integer employeeId=lactationBean.getEmployeeId();
		Integer requestType=lactationBean.getRequestType();
		Date startDate=lactationBean.getHbornStartDate();
		Date endDate=lactationBean.getEndDate();
		BigDecimal hours=lactationBean.getHours();
		
		Lactation lactation=new Lactation();
		lactation.setEmployeeId(employeeId);
		lactation.setHbornStartDate(startDate);
		lactation.setEndDate(endDate);
		lactation.setHours(hours);
		int record=lactationDao.insertSelective(lactation);
		if (record<=0) {
			return false;
		}
		//新增权限表----哺乳假权限
		RequestAuthorityExample requestAuthorityExample=new RequestAuthorityExample();
		requestAuthorityExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andRequestTypeEqualTo(requestType);
		List<RequestAuthority> list=requestAuthorityDao.selectByExample(requestAuthorityExample);
		if (list.size()==0) {//没有数据，执行插入
			RequestAuthority requestAuthority=new RequestAuthority();
			requestAuthority.setEmployeeId(employeeId);
			requestAuthority.setRequestType(requestType);
			record=requestAuthorityDao.insertSelective(requestAuthority);
			if (record<=0) {
				return false;
			}
		}
		//新增哺乳假t_request_report
		//两个日期循环
		Long startTime = startDate.getTime();
		Long endTime = endDate.getTime();
		Long oneDay = 1000 * 60 * 60 * 24l;  
		RequestReport requestReport=new RequestReport();
		requestReport.setEmployeeId(employeeId);
		requestReport.setHours(hours);
		requestReport.setReportType(10);
		while (startTime <= endTime) {
			Date requestReportDate = new Date(startTime);
			requestReport.setRequestReportDate(requestReportDate);
			requestReportDao.insertSelective(requestReport);
			startTime += oneDay;
		}
		//重计算考勤
		List<Integer> employeeIdList=new ArrayList<>();
		employeeIdList.add(employeeId);
		approvalService.resetAttendance(startDate, endDate, employeeIdList);
		return true;
	}

	/**
	 * 删除哺乳假
	 */
	@Override
	public boolean deleteLactation(String lactationId) {
		Lactation lactation=lactationDao.selectByPrimaryKey(Integer.parseInt(lactationId));
		int employeeId=lactation.getEmployeeId();
		Date startDate=lactation.getHbornStartDate();
		Date endDate=lactation.getEndDate();
		int record=lactationDao.deleteByPrimaryKey(Integer.parseInt(lactationId));
		if (record<=0) {
			return false;
		}
		//验证删除后是否还有哺乳假
		LactationExample lactationExample=new LactationExample();
		lactationExample.createCriteria().andEmployeeIdEqualTo(employeeId);
		List<Lactation> list=lactationDao.selectByExample(lactationExample);
		if (list.size()==0) {
			//没有哺乳假，删除哺乳假权限
			RequestAuthorityExample requestAuthorityExample=new RequestAuthorityExample();
			requestAuthorityExample.createCriteria()
			.andEmployeeIdEqualTo(employeeId)
			.andRequestTypeEqualTo(10);
			requestAuthorityDao.deleteByExample(requestAuthorityExample);
		}
		//删除type哺乳假t_request_report的信息
		RequestReportExample requestReportExample=new RequestReportExample();
		requestReportExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andReportTypeEqualTo(10)
		.andRequestReportDateBetween(startDate, endDate);
		requestReportDao.deleteByExample(requestReportExample);
		//重计算考勤
		List<Integer> employeeIdList=new ArrayList<>();
		employeeIdList.add(employeeId);
		approvalService.resetAttendance(startDate, endDate, employeeIdList);
		return true;
	}

	/**
	 * 修改哺乳假
	 */
	@Override
	public boolean updateLactation(LactationBean lactationBean) {
		//更新验证updatetime TODO
		int lactationId=lactationBean.getLactationId();
		int employeeId=lactationBean.getEmployeeId();
		Date startDate=lactationBean.getHbornStartDate();
		Date endDate=lactationBean.getEndDate();
		BigDecimal hours=lactationBean.getHours();
		Lactation lactation=lactationDao.selectByPrimaryKey(lactationId);
		if (!lactation.getUpdateTime().equals(lactationBean.getUpdateTime())) {
			throw new ResultFaultException("0_00011");
		}
		lactation.setHbornStartDate(lactationBean.getHbornStartDate());
		lactation.setEndDate(lactationBean.getEndDate());
		lactation.setHours(lactationBean.getHours());
		int record=lactationDao.updateByPrimaryKeySelective(lactation);
		if (record>0) {
			return true;
		}
		//删除type哺乳假t_request_report的信息
		RequestReportExample requestReportExample=new RequestReportExample();
		requestReportExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andReportTypeEqualTo(10)
		.andRequestReportDateBetween(startDate, endDate);
		requestReportDao.deleteByExample(requestReportExample);
		//新增哺乳假t_request_report
		//两个日期循环
		Long startTime = startDate.getTime();
		Long endTime = endDate.getTime();
		Long oneDay = 1000 * 60 * 60 * 24l;  
		RequestReport requestReport=new RequestReport();
		requestReport.setEmployeeId(employeeId);
		requestReport.setHours(hours);
		requestReport.setReportType(10);
		while (startTime <= endTime) {
			Date requestReportDate = new Date(startTime);
			requestReport.setRequestReportDate(requestReportDate);
			requestReportDao.insertSelective(requestReport);
			startTime += oneDay;
		}
		//重计算考勤
		List<Integer> employeeIdList=new ArrayList<>();
		employeeIdList.add(employeeId);
		approvalService.resetAttendance(startDate, endDate, employeeIdList);
		return false;
	}

	/**
	 * 查询哺乳假信息
	 */
	@Override
	public LactationPageBean getLactationList(LactationBean lactationBean) {
		LactationPageBean lactationPageBean=new LactationPageBean();
		lactationPageBean.setLactationBeanList(lactationExtDao.getLactationList(lactationBean));
		lactationPageBean.setPageTotal(lactationExtDao.count(lactationBean));;
		return lactationPageBean;
	}

	/**
	 * 根据ID查询哺乳假信息(用不到，好像)
	 */
	@Override
	public LactationBean getLactationListById(String lactationId) {
		// TODO Auto-generated method stub
		return null;
	}

}
